package cn.net.dpi.shp.service.impl;

import cn.hutool.core.io.FileUtil;
import cn.net.dpi.shp.service.ParseService;
import cn.net.dpi.shp.service.ShapefileService;
import com.linuxense.javadbf.DBFException;
import com.linuxense.javadbf.DBFReader;
import com.linuxense.javadbf.DBFRow;
import com.linuxense.javadbf.DBFUtils;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.util.*;

@Service
@Slf4j
public class ParseServiceImpl implements ParseService {

    @Autowired
    private List<ShapefileService> shapefileServiceList;

    @Override
    public String parserShpFile(String filePath) {
        File originalFile = new File(filePath);
        if(FileUtil.isEmpty(originalFile)) {
            return "文件不存在或目录为空";
        }
        if (FileUtil.isFile(originalFile)) {
            String path = originalFile.getAbsolutePath();
            String fileName = StringUtils.substringAfterLast(path, File.separator);
            String baseName = StringUtils.substringBeforeLast(fileName, ".").toLowerCase();
            String s1 = StringUtils.substringBeforeLast(path, File.separator);
            long fileId = Long.parseLong(StringUtils.substringAfterLast(s1, File.separator));
            for (ShapefileService ss : shapefileServiceList) {
                if (ss.parseShpfile(fileId, baseName, path, "1", 1, "baidu_shp")) {
                    break;
                }
            }
        }
        if (FileUtil.isDirectory(originalFile)) {
            Arrays.stream(FileUtil.ls(filePath))
                    .filter(f -> f.getName().endsWith(".shp"))
                    .forEach(f -> {
                        String path = f.getAbsolutePath();
                        String fileName = StringUtils.substringAfterLast(path, File.separator);
                        String baseName = StringUtils.substringBeforeLast(fileName, ".").toLowerCase();
                        String s1 = StringUtils.substringBeforeLast(path, File.separator);
                        long fileId = Long.parseLong(StringUtils.substringAfterLast(s1, File.separator));
                        for (ShapefileService ss : shapefileServiceList) {
                            if (ss.parseShpfile(fileId, baseName, path, "1", 1, "baidu_shp")) {
                                break;
                            }
                        }
                    });
        }
        return "解析入库成功";
    }

    @Override
    public List<Map<String, Object>> parseDbfFile(String filePath) {
        List<Map<String, Object>> resultList = new ArrayList<>();
        FileInputStream fis = null;
        DBFReader reader = null;
        try {
            fis = new FileInputStream(filePath);
            reader = new DBFReader(fis);
            int fieldCount = reader.getFieldCount();
            String[] fieldNames = new String[fieldCount];
            for (int i = 0; i < fieldCount; i++) {
                fieldNames[i] = reader.getField(i).getName();
            }

            DBFRow row;
            while ((row = reader.nextRow()) != null) {
                Map<String, Object> rowMap = new HashMap<>();
                for (int i = 0; i < fieldCount; i++) {
                    rowMap.put(fieldNames[i], row.getObject(i));
                }
                resultList.add(rowMap);
            }
        } catch (DBFException e) {
            e.printStackTrace();
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } finally {
            DBFUtils.close(reader);
            try {
                if (fis != null) {
                    fis.close();
                }
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
        return resultList;
    }
}




